package nl.boektrust.btsoep.client.wsdl;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import nl.boektrust.btsoep.server.wsdl.Book;
import nl.boektrust.btsoep.server.wsdl.BrowseAdapter;
import nl.boektrust.btsoep.server.wsdl.BrowseSoapServlet;
import nl.boektrust.btsoep.server.wsdl.jaxws.SearchBooks;
import nl.boektrust.btsoep.server.wsdl.jaxws.SearchBooksResponse;

/**
 * Java clone of the hello.php UI to the SOAP server. All those string quotes
 * are handled by"cat hello.php | sed 's/\"/\\\"/g' | sed 's/^/\"/g' | sed 's/$/\"+/g' >hello.php.txt"
 * 
 * @author frans
 * 
 */
public class HelloServlet extends HttpServlet {
	private static final long serialVersionUID = -2463253094978326814L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ServletOutputStream sos = resp.getOutputStream();
		sos.println("<html><head><title>" + this.getClass().getName() + "</title></head><body>");
		String minauteur = req.getParameter("minauteur");
		String maxauteur = req.getParameter("maxauteur");
		String minisbn = req.getParameter("minisbn");
		String maxisbn = req.getParameter("maxisbn");
		String mintitel = req.getParameter("mintitel");
		String maxtitel = req.getParameter("maxtitel");
		int offset = 0;
		try {
			offset = Integer.parseInt(req.getParameter("offset"));
		} catch (Exception e) {}
		int limit = 10;
		try {
			offset = Integer.parseInt(req.getParameter("limit"));
		} catch (Exception e) {}
		String form = "<p>"
				+ "Zoek formulier voor een kunstmatige data set. Die data set bevat 'boeken' met een auteur, isbn en titel. "
				+ "En is gevuld met de reeks van (auteur1000, isbn1000, titel1000) tot en met (auteur1999, isbn1999, titel1999)."
				+ "U kunt zoeken met een optionele onder (min) en boven grens (max)."
				+ "Voor het geval er teveel resultaten terug komen is er een maximum aantal 'limit' en een paginering met 'offset' ingebouwd..."
				+ "</p>" + "<p>" + "<form action='"
				+ this.getClass().getSimpleName()
				+ "' method='post'>"
				+ "<table border='1'>"
				+ "<tr>"
				+ "<th>auteur</th>"
				+ "<th>isbn</th>"
				+ "<th>titel</th>"
				+ "</tr>"
				+ "<tr>"
				+ "<td><input type='text' name='minauteur' value='"
				+ minauteur
				+ "' /></td>"
				+ "<td><input type='text' name='minisbn' value='"
				+ minisbn
				+ "' /></td>"
				+ "<td><input type='text' name='mintitel' value='"
				+ mintitel
				+ "' /></td>"
				+ "</tr>"
				+ "<tr>"
				+ "<td><input type='text' name='maxauteur' value='"
				+ maxauteur
				+ "' /></td>"
				+ "<td><input type='text' name='maxisbn' value='"
				+ maxisbn
				+ "' /></td>"
				+ "<td><input type='text' name='maxtitel' value='"
				+ maxtitel
				+ "' /></td>"
				+ "</tr>"
				+ "</table>"
				+ "<table border='1'>"
				+ "<tr>"
				+ "<th>offset</th><th>limit</th>"
				+ "</tr>"
				+ "<tr>"
				+ "<td><input type='text' name='offset' value='"
				+ offset
				+ "' /></td>"
				+ "<td><input type='text' name='limit' value='"
				+ limit
				+ "' /></td>"
				+ "</tr>"
				+ "</table>"
				+ "<input type='submit' value='Zoek'>" + "</form>" + "</p>";
		sos.println(form);
		SearchBooksResponse response = null;
		SearchBooks searchBooks = new SearchBooks();
		Book bookmin = new Book();
		Book bookmax = new Book();
		searchBooks.setArg0(bookmin);
		searchBooks.setArg1(bookmax);
		searchBooks.setArg2(offset);
		searchBooks.setArg3(limit);
		boolean direct = true;
		if (direct) {
			BrowseAdapter browseAdapter = new BrowseAdapter();
			response = browseAdapter.searchBooks(searchBooks);
		} else {
			// TODO: SOAP(searchBooks) -> BrowseSoapServlet.post -> response
			// =UNSOAP
			// TODO using reflection based wrapper {@link
			// nl.boektrust.btsoep.server.wsdl.TestBrowseSoapHandler}
			BrowseSoapServlet server = new BrowseSoapServlet();
			HttpServletRequest send = new HttpServletRequestWrapper(null);
			HttpServletResponse recv = new HttpServletResponseWrapper(null);
			server.doPost(send, recv);
		}
		String res = "<table border='1'>" + "<tr>" + "<th>auteur</th>" + "<th>isbn</th>" + "<th>titel</th>" + "</tr>";
		sos.println(res);
		for (Book b : response.getReturn()) {
			String s = "<tr><td>" + b.getAuteur() + "</td><td>" + b.getIsbn() + "</td><td>" + b.getTitel() + "</td>";
			sos.println(s);
		}
		sos.println("</table>");
		sos.println("</body></html>");
		sos.flush();
	}
}
